﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Contract.Domain;
using Framework.Domain;
using System.Reflection;

namespace CIIS.Common
{
    public class DataAccessObj
    {
        public static readonly DataAccessObj Instance = new DataAccessObj();

        DataAccessObj()
        {
        }

        private Contract.IService.IDaoService _dao;
        public Contract.IService.IDaoService Dao
        {
            get
            {
                try
                {
                    if (_dao == null)
                    {
                        _dao = (Contract.IService.IDaoService)ctx["DaoService"];
                    }
                    return _dao;
                }
                catch
                {
                    return null;
                }
            }
            set
            {
                _dao = value;
            }
        }

        private Spring.Context.IApplicationContext _ctx;
        protected Spring.Context.IApplicationContext ctx
        {
            get
            {
                try
                {
                    if (_ctx == null)
                        _ctx = Spring.Context.Support.ContextRegistry.GetContext();
                    return _ctx;
                }
                catch
                {
                    return null;
                }
            }
        }

        #region 数据及缓存操作
        IList GetDataList<T>()
        {
            return Utils.Instance.GetDataList<T>(Dao);
        }

        IList GetDataList(string tableName)
        {
            return Utils.Instance.GetDataList(Dao, tableName);
        }


        Dictionary<string, IList> listDictionary = new Dictionary<string, IList>();
        /// <summary>
        /// 从缓存中读数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IList GetListFromCache(string tableName)
        {
            IList dataList = null;
            if (listDictionary.ContainsKey(tableName))
            {
                dataList = listDictionary[tableName];
            }
            else
            {
                dataList = GetDataList(tableName);
                listDictionary[tableName] = dataList;
            }
            return dataList;
        }
        public IList GetListFromCache<T>() where T : Entity
        {
            return GetListFromCache(typeof(T).Name);
        }

        /// <summary>
        /// 重新加载数据，将旧数据从缓存中清除，再从数据库中重新读取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IList ReloadDataList(string tableName)
        {
            if (listDictionary.ContainsKey(tableName))
            {
                listDictionary.Remove(tableName);
            }

            return GetListFromCache(tableName);
        }
        public IList ReloadDataList<T>() where T : Entity
        {
            return ReloadDataList(typeof(T).Name);
        }

        #endregion

        #region 下拉框绑定参数
        Dictionary<string, DropdownControlParam> dcpDictionary = new Dictionary<string, DropdownControlParam>();
        /// <summary>
        /// 仅提供基础字典表的下拉参数
        /// </summary>
        /// <param name="baseTableName"></param>
        /// <returns></returns>
        public DropdownControlParam GetDropdownControlParam(string baseTableName)
        {
            if (baseTableName.StartsWith("bm_"))
            {
                DropdownControlParam dcp = new DropdownControlParam() { ValueMember = "BH0000", DisplayMember = "MC0000", Caption = string.Empty, DataSource = GetListFromCache(baseTableName) };
                return dcp;
            }
            else
            {
                return null;
            }
        }
        public DropdownControlParam GetDropdownControlParam<T>() where T : Entity
        {
            return GetDropdownControlParam(typeof(T).Name);
        }

        #endregion
    }
}
